﻿using Microsoft.WindowsAPICodePack.Taskbar;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WPFTaskbar.Dialog;

namespace WPFTaskbar.Examples
{
    /// <summary>
    /// Example1.xaml 的交互逻辑
    /// </summary>
    public partial class Example1 : Window
    {
        public Example1()
        {
            InitializeComponent();
        }

        private ImageSource IconToBitmapSource(Icon img)
        {
            Bitmap bitmap = img.ToBitmap();
            IntPtr hBitmap = bitmap.GetHbitmap();

            ImageSource bitmapSource =
                        Imaging.CreateBitmapSourceFromHBitmap(
                             hBitmap, IntPtr.Zero, Int32Rect.Empty,
                             BitmapSizeOptions.FromEmptyOptions());
            return bitmapSource;
        }  

        private void changeIcon_Click(object sender, EventArgs e)
        {
            taskBar.Overlay = IconToBitmapSource(new Icon("Images/3.ico"));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Icon icon = new Icon("Images/6.ico");
            TaskbarManager.Instance.SetOverlayIcon(icon, "Overlay Icon Demo");
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            Dialog1 newWindow = new Dialog1();
            newWindow.Show();
            WindowInteropHelper helper = new WindowInteropHelper(newWindow);
            IntPtr ptr = helper.Handle;
            TaskbarManager.Instance.SetApplicationIdForSpecificWindow(ptr, "AppID");
            Icon icon = new Icon("Images/5.ico");
            TaskbarManager.Instance.SetOverlayIcon(ptr, icon, "Overlay Icon Demo");
        }

        private void Button2_Click(object sender, RoutedEventArgs e)
        {
            int iconWidth = 20;
            int iconHeight = 20;

            for (int i = 10; i > 0; i--)
            {
                RenderTargetBitmap bmp = new RenderTargetBitmap(iconWidth, iconHeight, 96, 96, PixelFormats.Default);
                ContentControl ctl = new ContentControl();

                ctl.ContentTemplate = ((DataTemplate)Resources["DynamicIcon"]);
                ctl.Content = i.ToString();
                ctl.Arrange(new Rect(0, 0, iconWidth, iconHeight));

                bmp.Render(ctl);
                TaskbarItemInfo.Overlay = (ImageSource)bmp;
                Thread.Sleep(1000);
            }
        }
    }
}
